Ext.require('archives.common.Ajax');
Ext.define('archives.controller.GridController', {
	extend : 'Ext.app.Controller',

	init : function() {},

	getMainView: function () {
        var self = this;

        if(!self.viewInstance) {
            if(self.views && self.views.length) {

                var view = self.getView(self.mainView);

                self.viewInstance = view.create();

                self.viewInstance.close = function () {
                    view.prototype.close.apply(this, arguments);
                    self.viewInstance = null;
                };
                
                self.initMainView();
            } 
        }
        return self.viewInstance;
    },
    
	initMainView : function()  //subClass extend
	{
		this.getMainView().on('add', this.add, this);
		this.getMainView().on('modify', this.modify, this);
		this.getMainView().on('remove', this.remove, this);
		this.getMainView().on('refresh', this.refresh, this);
		this.getMainView().on('modifyRec', this.modifyRec, this);
		this.getMainView().on('detailRec', this.detailRec, this);
		this.getMainView().on('itemdblclick', this.gridDbClick, this);
		
		this.getMainView().getStore().on('beforeload',this.gridBeforeLoad,this);
	},
	
	refreshViewData : function()
	{
		this.getMainView().getStore().load();
	},
	
	gridBeforeLoad : function( store, operation, eOpts )  //subClass extend
	{
	},
	
	add : function(_this, tbar, grid) {
		var model = this.getModel(this.mainModel).create();
		this.winShow(this.mainEdit,model,G_OPERACTION.ADD);
	},
	modify : function(_this, tbar, grid) {
		
    	var item = this.getGridSingleItem();
		if (item == null) return;
		
		this.winShow(this.mainEdit,item,G_OPERACTION.MODIFY);
	},
	modifyRec : function(recIndex)
	{
		var model = this.getMainView().getStore().getAt(recIndex);
    	this.winShow(this.mainEdit,model,G_OPERACTION.MODIFY);
	},
	remove : function(_this, tbar, grid) {
		
		var items = this.getGridItems();
		if (items == null) return;
		
		Ext.MessageBox.confirm(_('Confirm'),_('Are you sure you want to delete selected items?'), function(e)
                        {
                            if (e == 'yes')
                            {                                
                            	var ids = this.getIdList(items);
								var url = G_SMPACKAGE + 'remove_'+AlUtil.getShortValue(this.mainModel)+'.action';
								AlAjax.postJsonString(url,ids,this.refresh,this,this.getMainView());
                            }
                        },this);
	},
	refresh : function(_this, tbar, grid) {
		this.refreshViewData();
	},
	detailRec : function(recIndex)
	{
		var model = this.getMainView().getStore().getAt(recIndex);
    	this.winShow(this.mainEdit,model,G_OPERACTION.DETAIL);
	},
	gridDbClick : function( _this, record, item, index, e, eOpts )
	{
		this.detailRec(index);
	},
	onSaveData : function(_this,win)
	{
		var formPanel = win.down('form').getForm();
		
		if (!this.formValid(win,formPanel))  
		{
	    	return false;
		}
		
		var record = formPanel.getRecord();
		formPanel.updateRecord(record);

		var url = G_SMPACKAGE + win.action+'_'+AlUtil.getShortValue(this.mainModel)+'.action';
		AlAjax.postModel(url,record,this.saveCallBack,this,win);
		
		return true;
	},
	saveCallBack : function(response,options, eOpts)
	{
		this.refresh();
		if (eOpts.body.action == G_OPERACTION.ADD)
		{
			this.resetWin(eOpts.body,this.getModel(this.mainModel).create());
		}
		else
		{
			eOpts.body.close();
		}
	},
	formValid : function(win,formPanel) //subClass extend
	{
		var result = formPanel.isValid();
		if (!result)
		{
			AlUtil.showMsg(_('form errors in data!'));
	    	return false;
		}
		return true;
	},
	
	getWindow : function(name)
	{
		if (this.winMap === null || this.winMap === undefined)
		{
			this.winMap = new Ext.util.HashMap();
		}
		var win = this.winMap.get(name);
		if (win == null)
		{
			win = this.getView(name).create();
			this.winMap.add(name,win);
			this.addWinListener(name);
		}
		return win;
	},
	addWinListener : function(winName)  //subClass extend
	{
		if (this.mainEdit === winName)
		{
			var win = this.winMap.get(winName);
			win.on('save',this.onSaveData,this);
		}
	},
	winShow : function(winName,record,action)
	{
		var win = this.getWindow(winName);
		win.action = action;
		this.resetWin(win,record);
		
		this.setWinStatus(win);
		//win.down('#ckStatus').setValue(true);
		win.show();
	},
	resetWin : function(win,record)
	{
		var form = win.down('form');
		if (form)
		{
			form.getForm().reset();
			if (record) form.loadRecord(record);
		}
	},
	getGridItems : function()
	{
		var items = this.getMainView().getSelectionModel().getSelection();
		if (!items || items.length == 0) {
			
	    	AlUtil.showMsg(_('Please check items !'));
	    	return null;
		}
		return items;
	},
	getGridSingleItem : function()
	{
		var items = this.getMainView().getSelectionModel().getSelection();
		if (!items || items.length != 1) {
			
	    	AlUtil.showMsg(_('please check one item!'));
	    	return null;
		}
		return items[0];
	},
	getIdList : function(items)
	{
		var ids = [];
		Ext.each(items, function(item) {
	    		    ids.push(item.internalId);
	    		});
	    return ids;
	},
	setWinStatus : function(win)
	{
		if (win.action == G_OPERACTION.DETAIL) 
		{	
			win.down('#buttonSave').hide();
		}
		else
		{
			win.down('#buttonSave').show();
		}
		var fields = win.down('form').getForm().getFields().items;
		Ext.Array.each(fields, function(field, index) {
			try
			{
		    	field.setReadOnly(win.action == G_OPERACTION.DETAIL);
			}
			catch(e)
			{
				//
			}
		});
	}
});